<!doctype html>
<html lang="zh" class="no-js">
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="generator" content="Hugo 0.75.1" />


<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
<meta name="google-site-verification" content="9Ph9Wm3pZwsQkEkjY7ZubNjjiPp6nODqkLZDjJgoaCY" />



<link rel="shortcut icon" href="/favicons/favicon.ico?" >
<link rel="apple-touch-icon" href="/favicons/apple-touch-icon-180x180.png" sizes="180x180">
<link rel="icon" type="image/png" href="/favicons/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicons/android-36x36.png" sizes="36x36">
<link rel="icon" type="image/png" href="/favicons/android-48x48.png" sizes="48x48">
<link rel="icon" type="image/png" href="/favicons/android-72x72.png" sizes="72x72">
<link rel="icon" type="image/png" href="/favicons/android-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/favicons/android-144x144.png" sizes="144x144">
<link rel="icon" type="image/png" href="/favicons/android-192x192.png" sizes="192x192">

<title>C&#43;&#43;序列化和反序列化工具 | 努力探求技术边界</title><meta property="og:title" content="C&#43;&#43;序列化和反序列化工具" />
<meta property="og:description" content="std::stringstream, std::strstream, std::iostream, std::streambuf 使用技巧" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://gqw.github.io/blog/2020/11/18/c-%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%B7%A5%E5%85%B7/" />
<meta property="article:published_time" content="2020-11-18T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-11-18T00:00:00+00:00" /><meta property="og:site_name" content="努力探求技术边界" />
<meta itemprop="name" content="C&#43;&#43;序列化和反序列化工具">
<meta itemprop="description" content="std::stringstream, std::strstream, std::iostream, std::streambuf 使用技巧">
<meta itemprop="datePublished" content="2020-11-18T00:00:00+00:00" />
<meta itemprop="dateModified" content="2020-11-18T00:00:00+00:00" />
<meta itemprop="wordCount" content="497">



<meta itemprop="keywords" content="" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="C&#43;&#43;序列化和反序列化工具"/>
<meta name="twitter:description" content="std::stringstream, std::strstream, std::iostream, std::streambuf 使用技巧"/>





<link rel="preload" href="/scss/main.min.276cf758ea51ad17445a946225f8c9b77391699167c3fb92541c8dfce581a44c.css" as="style">
<link href="/scss/main.min.276cf758ea51ad17445a946225f8c9b77391699167c3fb92541c8dfce581a44c.css" rel="stylesheet" integrity="">


<script
  src="https://code.jquery.com/jquery-3.5.1.min.js"
  integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
  crossorigin="anonymous"></script>



  </head>
  <body class="td-page td-blog">
    <header>
      
<nav class="js-navbar-scroll navbar navbar-expand navbar-dark flex-column flex-md-row td-navbar">
        <a class="navbar-brand" href="/">
		<span class="navbar-logo"><svg height="232pt" viewBox="0 0 232 232" width="232pt" xmlns="http://www.w3.org/2000/svg"><g transform="matrix(.1 0 0 -.1 0 232)"><path d="m1385 2213c-22-20-68-49-103-65-73-35-137-40-282-20-189 26-321-20-336-118-5-33-9-36-40-36-64 0-130-93-188-266-29-86-48-221-41-298 9-114 18-162 30-166 7-3 30-1 52 5l39 11 22 96c21 90 68 198 111 253l18 23 6-47c17-118 89-171 231-169 65 1 119 11 276 54 161 45 209 54 275 54 144 0 205-61 205-204v-57l61-12c34-7 62-11 64-9 10 10 25 144 25 223 0 173-46 299-144 4e2l-55 56-54-31c-62-35-89-40-37-7 75 47 141 138 156 215l7 32-58-35c-66-39-139-58-207-53l-47 3 20 45c19 42 44 148 37 155-2 2-21-13-43-32z"/><path d="m516 1230c-49-10-95-21-102-24-16-6-19-76-4-76 17 0 50-63 50-94 0-68 33-149 79-197 56-57 103-73 222-73 73 0 93 4 142 28 97 47 167 157 167 261 0 44 1 45 33 45s32 0 33-53c1-75 36-148 99-206 65-58 120-76 231-75 170 2 255 80 279 254 8 63 27 105 50 114 11 4 15 16 13 38-3 32-5 33-78 50-170 41-351 33-479-20-89-37-211-38-285-4-111 52-292 65-450 32zm385-59c78-30 114-70 113-126-1-62-47-150-97-186-53-39-108-52-188-47-131 10-198 78-207 212-6 91 12 129 71 157 58 27 226 22 308-10zm708 11c73-36 97-114 67-215-22-76-64-123-129-142-1e2-30-201-14-266 41-75 64-114 187-76 241 40 57 145 91 280 92 67 1 99-4 124-17z"/><path d="m783 1105c-29-20-46-75-34-107 11-30 57-68 81-68 35 0 78 22 89 46 14 32 14 85-2 105-30 39-96 51-134 24z"/><path d="m1473 1105c-29-20-46-75-34-106 5-13 19-35 31-48 19-19 30-22 64-18 54 7 86 44 86 97 0 70-89 116-147 75z"/><path d="m459 537c-25-7-59-23-76-35-89-63-92-230-6-301 60-49 134-59 231-29l32 10v74c0 59 3 74 15 74 8 0 15 5 15 10 0 6-33 10-80 10s-80-4-80-10c0-5 11-10 25-10 25 0 25-1 25-80v-80h-35c-77 0-125 71-125 182 1 116 53 183 138 176 34-3 37-6 51-50 10-34 20-48 33-48 15 0 18 8 18 45 0 38-4 47-23 54-45 17-112 20-158 8z"/><path d="m960 538c-77-21-124-92-124-188 0-84 31-145 91-178 29-16 45-33 53-56 15-48 84-73 150-57l35 9-41 1c-44 1-70 21-76 58-2 18 6 27 42 45 71 36 105 116 94 215-8 71-63 135-128 152-51 13-49 13-96-1zm89-18c32-16 51-83 51-173-1-134-39-192-116-173-77 20-90 288-17 344 20 14 57 16 82 2z"/><path d="m1367 525c18-13 24-26 38-85 2-8 22-72 43-142 22-70 37-130 34-133s6-5 21-5c24 0 28 7 52 88 48 162 62 198 69 181 4-9 24-72 44-140 31-103 41-125 58-127 18-3 24 10 53 110 74 260 70 248 96 248 14 0 25 5 25 10 0 6-27 10-60 10s-60-4-60-10c0-5 12-10 26-10 22 0 24-3 18-22-4-13-20-72-35-131-15-60-29-111-32-113-3-4-48 141-62 203-2 10-7 28-11 41-5 19-3 22 20 22 14 0 26 5 26 10 0 6-33 10-80 10-44 0-80-4-80-9s11-11 23-13l24-3-29-1e2c-16-55-31-111-34-124-11-52-18-41-46 65-42 159-42 164-18 164 11 0 20 5 20 10 0 6-34 10-82 10-76 0-81-1-61-15z"/></g></svg></span><span class="text-uppercase font-weight-bold">努力探求技术边界</span>
	</a>
	<div class="td-navbar-nav-scroll ml-md-auto" id="main_navbar">
		<ul class="navbar-nav mt-2 mt-lg-0">
			
			
			<li class="nav-item mr-4 mb-2 mb-lg-0">
				
				
				
				
				
				
				<a class="nav-link active" href="/blog/" ><span class="active">博客</span></a>
			</li>
			
			<li class="nav-item mr-4 mb-2 mb-lg-0">
				
				
				
				
				
				
				<a class="nav-link" href="/about/" ><span>关于</span></a>
			</li>
			
			
			
		</ul>
	</div>
	<div class="navbar-nav d-none d-lg-block">
<input type="search" class="form-control td-search-input" placeholder="&#xf002 站内搜索…" aria-label="站内搜索…" autocomplete="off">

</div>
</nav>

    </header>
    <div class="container-fluid td-outer">
      <div class="td-main">
        <div class="row flex-xl-nowrap">
          <div class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
            




<div id="td-sidebar-menu" class="td-sidebar__inner">
  
  <form class="td-sidebar__search d-flex align-items-center">
    
<input type="search" class="form-control td-search-input" placeholder="&#xf002 站内搜索…" aria-label="站内搜索…" autocomplete="off">


    <button class="btn btn-link td-sidebar__toggle d-md-none p-0 ml-3 fas fa-bars" type="button" data-toggle="collapse" data-target="#td-section-nav" aria-controls="td-docs-nav" aria-expanded="false" aria-label="Toggle section navigation">
    </button>
  </form>
  
  <nav class="collapse td-sidebar-nav" id="td-section-nav">
    
    






<ul class="td-sidebar-nav__section pr-md-3">
  <li class="td-sidebar-nav__section-title">
    <a  href="/blog/" class="align-left pl-0 pr-2 active td-sidebar-link td-sidebar-link__section">博客</a>
  </li>
  <ul>
    <li class="collapse show" id="blog">
      
      
      
      
      
      
      <a class="td-sidebar-link td-sidebar-link__page " id="m-blog20201229tcprt" href="/blog/2020/12/29/tcprt/">tcprt</a>
      
      
      
      
      
      <a class="td-sidebar-link td-sidebar-link__page  active" id="m-blog20201118c-e5ba8fe58897e58c96e5928ce58f8de5ba8fe58897e58c96e5b7a5e585b7" href="/blog/2020/11/18/c-%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E5%B7%A5%E5%85%B7/">C&#43;&#43;序列化和反序列化工具</a>
      
      
      
      
      
      <a class="td-sidebar-link td-sidebar-link__page " id="m-blog20201001e4bb8ehellowolde5bc80e5a78be6b7b1e585a5e6b585e587bac-20-coroutine-ts" href="/blog/2020/10/01/%E4%BB%8Ehellowold%E5%BC%80%E5%A7%8B%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BAc-20-coroutine-ts/">C&#43;&#43; 20 Coroutine</a>
      
      
    </li>
  </ul>
</ul>

  </nav>
</div>




          </div>
          <div class="d-none d-xl-block col-xl-2 td-toc d-print-none">
            






<div class="td-page-meta ml-2 pb-1 pt-2 mb-0" style="display: none">





<a href="https://github.com/gqw/edit/master/content/zh/blog/C&#43;&#43;/c&#43;&#43;%20%e5%ba%8f%e5%88%97%e5%8c%96%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96%e7%a5%9e%e5%99%a8.md" target="_blank"><i class="fa fa-edit fa-fw"></i> 编辑此页</a>
<a href="https://github.com/gqw/issues/new?title=C&#43;&#43;%e5%ba%8f%e5%88%97%e5%8c%96%e5%92%8c%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96%e5%b7%a5%e5%85%b7" target="_blank"><i class="fab fa-github fa-fw"></i> 提交文档问题</a>


<a href="https://github.com/gqw/issues/new" target="_blank"><i class="fas fa-tasks fa-fw"></i> 提交项目问题</a>

</div>






<nav id="TableOfContents">
  <ul>
    <li><a href="#最直接的方法">最直接的方法</a></li>
    <li><a href="#使用stringstream">使用stringstream</a></li>
    <li><a href="#我们还有strstream">我们还有strstream</a></li>
    <li><a href="#最后大招自己造轮子">最后大招，自己造轮子</a></li>
  </ul>
</nav>



          </div>
          <main class="col-12 col-md-9 col-xl-8 pl-md-5 pr-md-4" role="main">
            <a class="btn btn-lg td-rss-button d-none d-lg-block" href="https://gqw.github.io/blog/index.xml" target="_blank" >
             <img src="http://www.runoob.com/images/rss.gif" width="36" height="14">
            </a>
            
<div class="td-content">
	<h1>C&#43;&#43;序列化和反序列化工具</h1>
	<div class="lead">std::stringstream, std::strstream, std::iostream, std::streambuf 使用技巧</div>
	<div class="td-byline mb-4">
		By <b>顾起威 (<a href="https://gqw.github.io">@gqw</a>)</b> |
		<time datetime="2020-11-18" class="text-muted">2020-11-18</time>
	</div>
	
	<h1 id="c序列化和反序列化工具">C++序列化和反序列化工具</h1>
<p>在开发时我们经常会遇到需要序列号和反序列化得操作。比如将程序配置导出下次打开再从配置中读取，通常你可以将信息保存成json、xml或ini格式，让后再解析出来。更常见的情形是我们通过网络发送和接收数据，也涉及到序列化和反序列化操作，当让你可以使用protobuf、msgpack等序列化库。但是如果你只是个简单程序，或者序列化和反序列化的业务不是很重，不想搞得那么复杂有什么简单的方法吗？</p>
<h2 id="最直接的方法">最直接的方法</h2>
<p>直接按字节拷贝，见代码:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#8f5902;font-style:italic">#include</span> <span style="color:#8f5902;font-style:italic">&lt;string&gt;</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>
<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">static_cast</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>

	<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">size</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#0000cf;font-weight:bold">4</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">d</span><span style="color:#000;font-weight:bold">))</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">false</span><span style="color:#000;font-weight:bold">;</span>

	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">size_t</span> <span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">d</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">true</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>

<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> 
	<span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">out</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
	<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">size</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#0000cf;font-weight:bold">4</span><span style="color:#000;font-weight:bold">))</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">false</span><span style="color:#000;font-weight:bold">;</span>

	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">size_t</span> <span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">memcpy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">));</span>
	<span style="color:#000">pos</span> <span style="color:#ce5c00;font-weight:bold">+=</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#000;font-weight:bold">);</span>

	<span style="color:#000">out</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">pos</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">d</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87">true</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>


<span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">()</span>
<span style="color:#000;font-weight:bold">{</span>
	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">resize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;test&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">out</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">out</span><span style="color:#000;font-weight:bold">);</span>
	<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div><p>或者简单封装下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">TestData</span> <span style="color:#000;font-weight:bold">{</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000;font-weight:bold">...</span>
	<span style="color:#000;font-weight:bold">}</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#000;font-weight:bold">...</span>
	<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">};</span>

<span style="color:#000">TestData</span> <span style="color:#000">in</span><span style="color:#000;font-weight:bold">{</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;test&#34;</span> <span style="color:#000;font-weight:bold">};</span>
<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">resize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1024</span><span style="color:#000;font-weight:bold">);</span>
<span style="color:#000">in</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">);</span>

<span style="color:#000">TestData</span> <span style="color:#000">out</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">out</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">);</span>
</code></pre></div><h2 id="使用stringstream">使用stringstream</h2>
<p>可以看到上面的代码还是比较繁琐的，不仅需要手动计算字符索引位置，还要直接使用memcpy函数。对于这个问题c++的stringstream可以简化不少代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">TestData</span> <span style="color:#000;font-weight:bold">{</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">static_cast</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>

		<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">ostringstream</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#000">buf</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">str</span><span style="color:#000;font-weight:bold">();</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">good</span><span style="color:#000;font-weight:bold">();</span>
	<span style="color:#000;font-weight:bold">}</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">istringstream</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">);</span>
		<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span> <span style="color:#ce5c00;font-weight:bold">&gt;&gt;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">good</span><span style="color:#000;font-weight:bold">();</span>
	<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">};</span>
</code></pre></div><p>可以明显的看出代码比前面的要简洁优雅多了，但是这里面却有个比较严重的问题，无论是encode还是decode都需要构建stringstream，而stringstream会在内部构建一个新的streambuf对象，无论是输入还是输出都需要一次拷贝操作。</p>
<h2 id="我们还有strstream">我们还有strstream</h2>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">TestData</span> <span style="color:#000;font-weight:bold">{</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">static_cast</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>

		<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">ostrstream</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">good</span><span style="color:#000;font-weight:bold">();</span>
	<span style="color:#000;font-weight:bold">}</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">istrstream</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">seekp</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span> <span style="color:#8f5902;font-style:italic">// 设置写入位置（写入位置&gt;读取位置才代表有数据可读）
</span><span style="color:#8f5902;font-style:italic"></span>
		<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span> <span style="color:#ce5c00;font-weight:bold">&gt;&gt;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">good</span><span style="color:#000;font-weight:bold">();</span>
	<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">};</span>
</code></pre></div><p>stringstream使用的人比较多，但是知道strstream的就少的多了。strstream可以使用已有的内存做rdbuf, 但是很不幸的是这么好用的工具竟然被c++标准给废弃了<sup><a href="#ref_1">[1]</a></sup><sup><a href="#ref_2">[2]</a></sup>。</p>
<h2 id="最后大招自己造轮子">最后大招，自己造轮子</h2>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cpp" data-lang="cpp"><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">serialization_streambuf</span> <span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">streambuf</span>
<span style="color:#000;font-weight:bold">{</span>
	<span style="color:#000">serialization_streambuf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">size_t</span> <span style="color:#000">n</span><span style="color:#000;font-weight:bold">)</span>
	<span style="color:#000;font-weight:bold">{</span>
		<span style="color:#000">setp</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">n</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">// set write buffer pointer
</span><span style="color:#8f5902;font-style:italic"></span>		<span style="color:#000">setg</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">n</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">// set read buffer pointer
</span><span style="color:#8f5902;font-style:italic"></span>	<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">};</span>

<span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">TestData</span> <span style="color:#000;font-weight:bold">{</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
	<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">static_cast</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87;font-weight:bold">int32_t</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>

		<span style="color:#000">serialization_streambuf</span> <span style="color:#000">osrb</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>
		<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">ostream</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">osrb</span><span style="color:#000;font-weight:bold">);</span>

		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">write</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">good</span><span style="color:#000;font-weight:bold">();</span>
	<span style="color:#000;font-weight:bold">}</span>

	<span style="color:#204a87;font-weight:bold">bool</span> <span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">string</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
		<span style="color:#000">serialization_streambuf</span> <span style="color:#000">osrb</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">length</span><span style="color:#000;font-weight:bold">());</span>
		<span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">istream</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">osrb</span><span style="color:#000;font-weight:bold">);</span>

		<span style="color:#204a87;font-weight:bold">int32_t</span> <span style="color:#000">d</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">b</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">((</span><span style="color:#204a87;font-weight:bold">char</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">d</span><span style="color:#000;font-weight:bold">));</span>
		<span style="color:#000">ss</span> <span style="color:#ce5c00;font-weight:bold">&gt;&gt;</span> <span style="color:#000">s</span><span style="color:#000;font-weight:bold">;</span>
		<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ss</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">good</span><span style="color:#000;font-weight:bold">();</span>
	<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">};</span>
</code></pre></div><p>可以看到，其实也很简单。就是模仿strstream为iostream构建streambuf就行了。这样不仅享受到了iostream类给我们提供的便利，也克服了stringstream需要内存拷贝的缺陷。</p>
<h1 id="参考引用">参考引用</h1>
<p>[1]  <a id="ref_1" href="https://en.cppreference.com/w/cpp/io/strstream" >std::strstream</a><code>[EB/OL].</code>  <a href="https://en.cppreference.com/w/cpp/io/strstream">https://en.cppreference.com/w/cpp/io/strstream</a></p>
<p>[2]  <a id="ref_2" href="https://stackoverflow.com/questions/2820221/why-was-stdstrstream-deprecated" >Why was std::strstream deprecated?</a><code>[EB/OL].</code>  <a href="https://stackoverflow.com/questions/2820221/why-was-stdstrstream-deprecated">https://stackoverflow.com/questions/2820221/why-was-stdstrstream-deprecated</a></p>

	

	<ul class="list-unstyled d-flex justify-content-between align-items-center mb-0 pt-5">
  <li>
    <a href="/blog/2020/10/01/%E4%BB%8Ehellowold%E5%BC%80%E5%A7%8B%E6%B7%B1%E5%85%A5%E6%B5%85%E5%87%BAc-20-coroutine-ts/" class="btn btn-primary "><span class="mr-1">←</span> 上一页</a>
  </li>
    <a href="/blog/2020/12/29/tcprt/" class="btn btn-primary ">下一页 <span class="ml-1">→</span></a>
  </li>
</ul>

</div><div id="utter-container"></div>
    <script src="https://utteranc.es/client.js"
        repo= 'gqw/hugocomment'
        issue-term= "title"
        theme= 'github-light'
        crossorigin= "anonymous"
        async>
    </script>


          </main>
        </div>
      </div>
      
<footer class="bg-dark py-5 row d-print-none">
  <div class="container-fluid mx-sm-5">
    <div class="row">
      <div class="col-6 col-sm-4 text-xs-center order-sm-2">
        
        
        
<ul class="list-inline mb-0">
  
  <li class="list-inline-item mx-2 h3" data-toggle="tooltip" data-placement="top" title="User mailing list" aria-label="User mailing list">
    <a class="text-white" target="_blank" href="gqwmail@qq.com">
      <i class="fa fa-envelope"></i>
    </a>
  </li>
  
</ul>

        
        
      </div>
      <div class="col-6 col-sm-4 text-right text-xs-center order-sm-3">
        
        
        
<ul class="list-inline mb-0">
  
  <li class="list-inline-item mx-2 h3" data-toggle="tooltip" data-placement="top" title="GitHub" aria-label="GitHub">
    <a class="text-white" target="_blank" href="https://github.com/gqw">
      <i class="fab fa-github"></i>
    </a>
  </li>
  
</ul>

        
        
      </div>
      <div class="col-12 col-sm-4 text-center py-2 order-sm-2">
        <small class="text-white">&copy; 2020 The Docsy Authors All Rights Reserved</small>
        
		
			<p class="mt-2"><a href="/about/">关于</a></p>
		
      </div>
    </div>
  </div>
</footer>


<script type="text/javascript"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [['$','$']],
      displayMath: [['$$','$$']],
      processEscapes: true,
      processEnvironments: true,
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
      TeX: { equationNumbers: { autoNumber: "AMS" },
          extensions: ["AMSmath.js", "AMSsymbols.js"] }
      }
  });
  </script>

    </div>
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>







<script src="/js/main.min.b99ff36446a5ac4634439b55123fb4bc85e1a0423d2b19b088890d1e33f503a6.js" integrity="sha256-uZ/zZEalrEY0Q5tVEj&#43;0vIXhoEI9KxmwiIkNHjP1A6Y=" crossorigin="anonymous"></script>



  </body>
</html>